using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WorkFlowCore.BusinessDemo.Web.Data;
using WorkFlowCore.BusinessDemo.Web.Entities;
using WorkFlowCore.BusinessDemo.Web.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WorkFlowCore.BusinessDemo.Web.Controllers
{
    [Route("api/user-select")]
    public class UserSelectForUserController : BaseController
    {
        private readonly BusinessDemoDbContext _context;

        public UserSelectForUserController(BusinessDemoDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取用户选项列表
        /// </summary>
        /// <returns>用户选项列表 [{"Id":"","Name":""}]</returns>
        [HttpGet("users")]
        public async Task<IActionResult> GetSelections()
        {
            var users = await _context.Users
                .Select(u => new Models.Selection
                {
                    Id = u.EmployeeNumber,
                    Name = u.Name
                })
                .ToListAsync();
            
            return Ok(users);
        }

        /// <summary>
        /// 根据用户ID获取用户详情
        /// </summary>
        /// <param name="input">选择器输入参数</param>
        /// <returns>用户详情 {"Id":"","Name":""}</returns>
        [HttpPost("user-detail")]
        public async Task<IActionResult> GetUser([FromBody] SelectorInput input)
        {

            var users = await _context.Users
                .Where(u => u.EmployeeNumber == input.SelectionId)
                .Select(u => new Models.UserSelection
                {
                    Id = u.EmployeeNumber,
                    Name = u.Name
                })
                .ToArrayAsync();

            return Ok(users);
        }
    }
}
